<template>
  <div class="app-container">
    <el-tabs v-model="activeTab" @tab-click="handleTabClick">
      <el-tab-pane label="全部公告" name="all">
        <el-table
          v-loading="listLoading"
          :data="announcements"
          border
          fit
          highlight-current-row
          style="width: 100%;"
        >
          <el-table-column label="标题" min-width="150px">
            <template slot-scope="{row}">
              <router-link :to="'/announcement/detail/'+row.id" class="link-type">
                <span>{{ row.title }}</span>
              </router-link>
              <el-tag v-if="row.isTop === 1" type="danger" size="mini" style="margin-left: 5px">置顶</el-tag>
              <el-tag v-if="!row.hasRead" type="warning" size="mini" style="margin-left: 5px">未读</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="类型" width="100px" align="center">
            <template slot-scope="{row}">
              <el-tag :type="row.type | typeFilter">{{ row.type | typeNameFilter }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="发布人" width="120px" align="center">
            <template slot-scope="{row}">
              <span>{{ row.publisherName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="发布部门" width="120px" align="center">
            <template slot-scope="{row}">
              <span>{{ row.departmentName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="发布时间" width="160px" align="center">
            <template slot-scope="{row}">
              <span>{{ row.publishTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
            <template slot-scope="{row}">
              <el-button type="primary" size="mini" @click="viewAnnouncement(row)">
                查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="未读公告" name="unread">
        <el-table
          v-loading="listLoading"
          :data="unreadAnnouncements"
          border
          fit
          highlight-current-row
          style="width: 100%;"
        >
          <el-table-column label="标题" min-width="150px">
            <template slot-scope="{row}">
              <router-link :to="'/announcement/detail/'+row.id" class="link-type">
                <span>{{ row.title }}</span>
              </router-link>
              <el-tag v-if="row.isTop === 1" type="danger" size="mini" style="margin-left: 5px">置顶</el-tag>
              <el-tag type="warning" size="mini" style="margin-left: 5px">未读</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="类型" width="100px" align="center">
            <template slot-scope="{row}">
              <el-tag :type="row.type | typeFilter">{{ row.type | typeNameFilter }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="发布人" width="120px" align="center">
            <template slot-scope="{row}">
              <span>{{ row.publisherName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="发布部门" width="120px" align="center">
            <template slot-scope="{row}">
              <span>{{ row.departmentName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="发布时间" width="160px" align="center">
            <template slot-scope="{row}">
              <span>{{ row.publishTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
            <template slot-scope="{row}">
              <el-button type="primary" size="mini" @click="viewAnnouncement(row)">
                查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getUserAnnouncements } from '@/api/announcement/index'

export default {
  name: 'AnnouncementUserCenter',
  filters: {
    typeFilter(type) {
      const typeMap = {
        1: 'primary',
        2: 'success',
        3: 'warning'
      }
      return typeMap[type]
    },
    typeNameFilter(type) {
      const typeMap = {
        1: '通知',
        2: '通告',
        3: '提醒'
      }
      return typeMap[type]
    }
  },
  data() {
    return {
      activeTab: 'all',
      announcements: [],
      unreadAnnouncements: [],
      listLoading: true
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getUserAnnouncements().then(response => {
        this.announcements = response.data
        this.unreadAnnouncements = this.announcements.filter(item => !item.hasRead)
        this.listLoading = false
      })
    },
    handleTabClick() {
      // 切换标签时可以添加额外逻辑
    },
    viewAnnouncement(row) {
      this.$router.push(`/announcement/detail/${row.id}`)
    }
  }
}
</script>

<style scoped>
.link-type {
  color: #337ab7;
}
</style> 